﻿using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuDAL.Repositories.DatabaseRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyGiaoVuService.ReportServices
{
    public class GiangVienReportService: IGiangVienReportService
    {
        IGiangVienRepository _giangVienRepository;
        IMonHocRepository _monHocRepository;

        public GiangVienReportService(IGiangVienRepository giangvienRepository, IMonHocRepository monHocRepository)
        {
            _giangVienRepository = giangvienRepository;
            _monHocRepository = monHocRepository;
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.GiangVienDTO> GetAllGiangVien()
        {
            return _giangVienRepository.GetAllGiangVien();
        }

        public QuanLyGiaoVuDAL.DTO.GiangVienDTO GetGiangVienById(Guid id)
        {
            return _giangVienRepository.GetGiangVienById(id);
        }

        public IEnumerable<ChucVuDTO> GetAllChucVu()
        {
            var danhSachChucVu = _giangVienRepository.GetAllChucVu().ToList();
            ChucVuDTO chucvuDTO = new ChucVuDTO();
            chucvuDTO.MaChucVu = Guid.Empty;
            chucvuDTO.TenChucVu = "Không có";
            danhSachChucVu.Insert(0,chucvuDTO);
            return danhSachChucVu.AsEnumerable();
        }
        public IEnumerable<ChucVuDTO> GetAllChucVuGiangVien(Guid id)
        {
            return _giangVienRepository.GetAllChucVuGiangVien(id);
        }
        public IEnumerable<HocViDTO> GetAllHocVi()
        {
            return _giangVienRepository.GetAllHocVi();
        }

        public IEnumerable<HocHamDTO> GetAllHocHam()
        {
            var danhSachHocHam = _giangVienRepository.GetAllHocHam().ToList();
            HocHamDTO hocHam = new HocHamDTO();
            hocHam.MaHocHam = -1;
            hocHam.TenHocHam = "Không có";
            danhSachHocHam.Insert(0, hocHam);
            return danhSachHocHam.AsEnumerable();
        }

        public IEnumerable<MonHocDTO> GetAllMonHocByGiangVienId(Guid giangVienId)
        {
            return _giangVienRepository.GetAllMonHocByGiangVienId(giangVienId);
        }

        public IEnumerable<MonHocDTO> GetAllMonHocChuaChonByGiangVienId(Guid giangVienId, Guid? maLop)
        {
            List<MonHocDTO> danhSachMonHoc = _monHocRepository.GetAllMonHoc(maLop).ToList();
            IEnumerable<MonHocDTO> danhSachMonHocTheoGiangVien = _giangVienRepository.GetAllMonHocByGiangVienId(giangVienId);
            foreach (MonHocDTO monhoc in danhSachMonHocTheoGiangVien)
            {
                int index = danhSachMonHoc.FindIndex(m => m.ID == monhoc.ID);
                danhSachMonHoc.RemoveAt(index);
            }
            return danhSachMonHoc.AsEnumerable();
        }


        public ChucVuDTO GetChucVuByChucVuId(Guid chucVuId)
        {
            return _giangVienRepository.GetChucVuByMaChucVu(chucVuId);
        }


        public IEnumerable<ChucVuDTO> GetAllChucVuChuaKiemNhiemByGiangVienId(Guid giangVienId)
        {
            var dsChucVu = _giangVienRepository.GetAllChucVu().ToList();
            var dsChucVuKiemNhiem = _giangVienRepository.GetAllChucVuGiangVien(giangVienId);
            foreach (ChucVuDTO chucVu in dsChucVuKiemNhiem)
            {
                var index = dsChucVu.FindIndex(cv => cv.MaChucVu == chucVu.MaChucVu);
                if (index != -1)
                    dsChucVu.RemoveAt(index);
            }
            return dsChucVu;
        }


        public IEnumerable<ChucDanhDTO> GetAllChucDanh()
        {
            return _giangVienRepository.GetAllChucDanh();
        }

        public ChucDanhDTO GetChucDanhByMaChucDanh(Guid maChucDanh)
        {
            return _giangVienRepository.GetChucDanhByMaChucDanh(maChucDanh);
        }


        public LichRanhDTO GetLichRanhById(Guid lichRanhId)
        {
            return _giangVienRepository.GetLichRanhById(lichRanhId);
        }

        public IEnumerable<LichRanhDTO> GetAllLichRanh(Guid? maGiangVien, bool? buoi, int? thu)
        {
            return _giangVienRepository.GetAllLichRanh(maGiangVien, buoi, thu);
        }


        public IList<string> GetEmailListByGiangVienIdList(String giangVienIdList)
        {
            List<String> emailList = new List<string>();
            string[] giangVienIds = giangVienIdList.Split(',');
            foreach (string giangVienIdString in giangVienIds)
            {
                Guid giangVienId;
                if (Guid.TryParse(giangVienIdString, out giangVienId))
                {
                    var giangVienDTO = _giangVienRepository.GetGiangVienById(giangVienId);
                    if (!string.IsNullOrEmpty(giangVienDTO.Email))
                        emailList.Add(giangVienDTO.Email);
                }
            }
            return emailList;
        }


        public GiangVienDTO GetGiangVienByEmail(string email)
        {
            return _giangVienRepository.GetGiangVienByEmail(email);
        }


        public IEnumerable<BoMonDTO> GetAllBoMon()
        {
            return _giangVienRepository.GetAllBoMon();
        }
    }
}
